///**
// * @author: fanzhiming
// * @date: 2016年12月13日下午2:52:36
// * @description:
// * @modify:
// */
//package com.topscomm.pm.quartz;
//
//import java.io.File;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//import org.quartz.JobExecutionContext;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import com.topscomm.cbo.CboSystemConst.CboMessageStateEnum;
//import com.topscomm.cbo.pojo.CboAttachmentEntity;
//import com.topscomm.cbo.pojo.CboMessageMailEntity;
//import com.topscomm.cbo.pojo.CboUserEntity;
//import com.topscomm.cbo.service.ICboAttachmentService;
//import com.topscomm.cbo.service.ICboMessageMailService;
//import com.topscomm.cbo.service.ICboUserService;
//import com.topscomm.message.MessageServiceFactory;
//import com.topscomm.message.mail.MailMessageEntity;
//import com.topscomm.pub.util.FileUtil;
//import com.topscomm.pub.util.I18nUtil;
//import com.topscomm.pub.util.StringBufferProxy;
//import com.topscomm.pub.util.StringUtil;
//import com.topscomm.pub.vo.ResponseResult;
//import com.topscomm.tap.common.ParameterCache;
//import com.topscomm.tap.quartz.TapJob;
//import com.xxl.job.core.biz.model.ReturnT;
//import com.xxl.job.core.handler.annotation.JobHandler;
//
///**
// * 邮件发送
// *
// * @description:
// * @author: donghang
// * @date: 2020年5月15日下午5:32:55
// * @modify:
// */
////@JobHandler(value = "cboMessageMailJob")
//// @Component
//public class CboMessageMailJob extends TapJob {
//	/**
//	 * 日志
//	 */
//	private final static Logger logger = LoggerFactory.getLogger(CboMessageMailJob.class);
//	/**
//	 * 邮件服务
//	 */
//	@Autowired
//	private ICboMessageMailService cboMessageMailService;
//	/**
//	 * 用户服务
//	 */
//	@Autowired
//	private ICboUserService cboUserService;
//	/**
//	 * 附件服务
//	 */
//	@Autowired
//	private ICboAttachmentService cboAttachmentService;
//	/**
//	 * 消息服务工厂
//	 */
//	@Autowired
//	MessageServiceFactory messageServiceFactory;
//
//	/**
//	 * 调度执行
//	 *
//	 * @description:
//	 * @param param
//	 * @return
//	 * @throws Exception
//	 * @author: donghang
//	 * @date: 2020年5月15日下午5:32:47
//	 * @modify:
//	 */
//	@Override
//	public void run(JobExecutionContext jobExecutionContext) {
//		try {
//			// 查询待发送消息
//			int everyQueryCount = ParameterCache.getIntValue("CboMailSendQueryCount", 100);
//			StringBufferProxy sql = new StringBufferProxy();
//			sql.appendSingle(" state = {0} and sendcount<3 ", CboMessageStateEnum.Sending);
//			List<CboMessageMailEntity> listMessage = cboMessageMailService.queryForPage(sql.toString(), 1,
//					everyQueryCount, "createOn", "asc");
//			// 未存在待发消息
//			if (listMessage.size() <= 0) {
//				return ;
//			}
//			for (CboMessageMailEntity message : listMessage) {
//				sendMessage(message);
//			}
//		} catch (Exception ex) {
//			logger.error("Error：", ex);
//		} finally {
//			try {
//				FileUtil.deleteDirectory(System.getProperty("user.dir").replace("bin", "webapps") + "/mailZip/", false);
//			} catch (Exception ex) {
//				logger.error("Error：", ex);
//			}
//		}
//		return ;
//	}
//
//	/**
//	 * 发送邮件
//	 *
//	 * @description:
//	 * @param message
//	 * @author: donghang
//	 * @date: 2020年5月18日下午5:18:58
//	 * @modify:
//	 */
//	private void sendMessage(CboMessageMailEntity message) {
//		try {
//			StringBufferProxy sql = new StringBufferProxy();
//			// 发送次数加1
//			message.setSendcount(message.getSendcount() + 1);
//			// 设置发送时间
//			message.setSenddate(new Date());
//			// 查询接收人邮箱
//			StringBufferProxy receiverMails = new StringBufferProxy();
//			if (StringUtil.isEmpty(message.getReceivermails())) {
//				sql.appendSingle(" id in ({0}) ", message.getReceiverids());
//				List<CboUserEntity> listReceiverUser = cboUserService.queryByWhere(sql.toString());
//				for (CboUserEntity receiverUser : listReceiverUser) {
//					// 不需要邮件通知 2019-04-02 fanzm
//					if ("1".equals(receiverUser.getExtendfield3()))
//						continue;
//
//					if (!StringUtil.isEmpty(receiverUser.getEmail())) {
//						receiverMails.appendSingle("{0},", receiverUser.getEmail());
//					}
//				}
//				if (receiverMails.length() > 0) {
//					receiverMails = receiverMails.substring(0, receiverMails.length() - 1);
//				}
//			} else {
//				receiverMails.appendSingle(message.getReceivermails());
//			}
//			if (receiverMails.length() <= 0) {
//				message.setState(CboMessageStateEnum.UnNeedSend);// 无需发送
//				message.setFailreason(I18nUtil.getMessage("tap.message.usermailboxnotexist"));
//				return;
//			}
//			message.setReceivermails(receiverMails.toString());
//			// 转成邮件发送消息
//			MailMessageEntity mailMessageEntity = new MailMessageEntity();
//			mailMessageEntity.setContent(message.getContent());
//			mailMessageEntity.setTitle(message.getSubject());
//			mailMessageEntity.setMailTo(message.getReceivermails());
//			// 邮件附件
//			List<CboAttachmentEntity> listAttachment = new ArrayList<CboAttachmentEntity>();
//			if (!StringUtil.isEmpty(message.getAttachmentids())) {
//				sql.appendSingle(" id in ({0}) ", message.getAttachmentids());
//				listAttachment = cboAttachmentService.queryByWhere(sql.toString());
//			}
//			List<String> listAttachmentPath = new ArrayList<String>();
//			for (CboAttachmentEntity attachment : listAttachment) {
//				String filePath = this.cboAttachmentService.downloadForMail(attachment);
//				if (!StringUtil.isEmpty(filePath)) {
//					listAttachmentPath.add(filePath);
//				}
//			}
//			List<File> fileList = new ArrayList<>();
//			for (String attachmentPath : listAttachmentPath) {
//				File file = new File(attachmentPath);
//				fileList.add(file);
//			}
//			mailMessageEntity.setAttachments(fileList);
//			// 发送消息
//			ResponseResult<Object> result = this.messageServiceFactory.sendMessage(mailMessageEntity);
//			if (result.isSuccess() == true) {
//				message.setState(CboMessageStateEnum.Success);// 发送成功
//			} else {
//				message.setState(CboMessageStateEnum.Fail);// 失败
//				message.setFailreason(result.getMessage());
//			}
//			message.setState(CboMessageStateEnum.Success);
//		} catch (Exception ex) {
//			message.setState(CboMessageStateEnum.Fail);// 失败
//			message.setFailreason(I18nUtil.getMessage("tap.message.failreason") + ":" + ex.getMessage());
//		} finally {
//			cboMessageMailService.updateEntity(message);
//		}
//	}
//
//}
